var flag=false;
Page({
  data:{
    name:"请填写您的姓名",
    tel:"请填写您的联系方式",
    addreValue:0,
    region: [],
    door:"街道门牌信息",
    index:"0",
  },
   onLoad: function(options) {
    this.setData({
    name:options.name,
    tel:options.tel,
    region:options.region,
    addre:options.region+options.door,
    addreValue:options.addrevalue,
    areaValue:options.areavalue,
    door:options.door,
    index:options.index
    })
    console.log("传过来的index"+options.index);
    console.log("传过来的addre"+options.addre);
    console.log("接收到的addre"+this.data.region);
    console.log("接收到的index"+this.data.index);
   },
   bindRegionChange: function (e) {
    
    console.log(e.detail.value)
    console.log(e.detail.code)
    ///////////////

    this.setData({
      region: e.detail.value
    })
  },
  bindNameChange:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  bindDoorChange:function(e){

    this.setData({
      door:e.detail.value
    })
  },
  bindTelChange:function(e){

    this.setData({
    tel:e.detail.value
    })
  },
  //点击删除
    delete:function(){
      var that = this;
      wx.showModal({
       title: '提示',
       content: '确认删除该地址信息吗？',
       
       success: function(res) {
         if (res.confirm) {
           console.log('用户点击确定')
           wx.request({
            url: 'http://82.157.94.115:2005/deleteaddress',
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              name:that.data.name,
            },
            success:function(res){
              console.log('成功')
              console.log(res.data)
            }
          })
            wx.redirectTo({
             url: '../chooseAddre/chooseAddre?name='+that.data.name+"&sign="+'2'
             }); 
         } else if (res.cancel) {
           console.log('用户点击取消')
      }
    }
})
    },
//点击取消，返回上个页面
    cancel:function(){
      wx.navigateBack({
         delta: 1
      })
    },
    //点击保存
  formSubmit: function(e) {
    var warn ="";
    var that = this;
    if(e.detail.value.name==""){
      warn = "请填写您的姓名！";
    }else if(e.detail.value.tel==""){
      warn = "请填写您的手机号！";
    }else if(!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.tel))){
      warn = "手机号格式不正确";
    }else if(e.detail.value.region==''){
      warn = "请选择您的所在区域";
    }else if(e.detail.value.door==""){
      warn = "请输入您的具体地址";
    }else{
      flag=true;
      console.log('form发生了submit事件，携带数据为：', e.detail.value.region)
      let pages = getCurrentPages();  
      let prevPage = pages[ pages.length - 2 ];

      prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
            name:e.currentTarget.dataset.name,
            region: e.currentTarget.dataset.region,
            door: e.currentTarget.dataset.door,
            tel: e.currentTarget.dataset.tel,
            flag:this.flag
      })
      wx.navigateBack({
        delta: 1,
      })
      //  wx.redirectTo({
      //   // url: '../chooseAddre/chooseAddre?tel='+e.detail.value.tel+"&addre="+that.data.addreRange[e.detail.value.addre]+"&door="+e.detail.value.door+"&name="+e.detail.value.name+"&area="+that.data.areaRange[e.detail.value.area]+"&flag="+flag+"&areavalue="+e.detail.value.area+"&addrevalue="+e.detail.value.addre+"&door="+e.detail.value.door
      //   url: '../chooseAddre/chooseAddre?tel='+e.detail.value.tel+"&region="+e.detail.value.region+"&door="+e.detail.value.door+"&name="+e.detail.value.name+"&flag="+flag
      //   //？后面跟的是需要传递到下一个页面的参数

      // }); 
      wx.request({
        url: 'http://82.157.94.115:2005/updateaddress',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          name:e.currentTarget.dataset.name,
          tel:e.currentTarget.dataset.tel,
          region:e.currentTarget.dataset.region,
          door:e.currentTarget.dataset.door,
        },
        success:function(){
          console.log('成功')
          console.log(that.data.name)
          console.log(that.data.region)
          console.log(that.data.tel)
          console.log(that.data.door)
        }
      })
      //   wx.redirectTo({
      //   // url: '../chooseAddre/chooseAddre?tel='+e.detail.value.tel+"&addre="+that.data.addreRange[e.detail.value.addre]+"&door="+e.detail.value.door+"&name="+e.detail.value.name+"&area="+that.data.areaRange[e.detail.value.area]+"&sign="+'1'+"&areavalue="+e.detail.value.area+"&addrevalue="+e.detail.value.addre+"&index="+that.data.index
      //   url: '../chooseAddre/chooseAddre?tel='+e.detail.value.tel+"&addre="+e.detail.value.addre+"&name="+e.detail.value.name+"&region="+e.detail.value.region+"&door="+e.detail.value.door+"&sign="+'1'+"&index="+that.data.index
      //   //？后面跟的是需要传递到下一个页面的参数
      // }); 
    }
    if(flag==false){
      wx.showModal({
      title: '提示',
      content:warn
    })
    }
    
  },
  
  })
